﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DaInterFace;
using DaInterFace.SYN;
using DaInterFace.SYN.E01;

namespace E01
{
    /// <summary>
    /// 同步销售计量方案
    /// </summary>
    public class A01 : ISYNChildSet
    {
        public void Code(ISYN handler, CodeResult result, SYNRight user, SerialType st, String parameter)
        {
            E01A01P P = (E01A01P)handler.Serial.Deserialize(st, typeof(E01A01P), parameter);

            try
            {
                SqlParameter[] sps = new SqlParameter[14];
                sps[0] = new SqlParameter("@ID", SqlDbType.NVarChar, 50);
                sps[0].Value = P.ID;
                sps[1] = new SqlParameter("@Number", SqlDbType.NVarChar, 50);
                sps[1].Value = P.Number;
                sps[2] = new SqlParameter("@Name", SqlDbType.NVarChar, 50);
                sps[2].Value = P.Name;
                sps[3] = new SqlParameter("@IsBuildStockOut", SqlDbType.Bit);
                sps[3].Value = P.IsBuildStockOut;
                sps[4] = new SqlParameter("@NeedCheckOut", SqlDbType.Bit);
                sps[4].Value = P.NeedCheckOut;
                sps[5] = new SqlParameter("@MinSpan", SqlDbType.Int);
                sps[5].Value = P.MinSpan;
                sps[6] = new SqlParameter("@EmptyMinSpan", SqlDbType.Decimal);
                sps[6].Value = P.EmptyMinSpan;
                sps[7] = new SqlParameter("@MinSuttle", SqlDbType.Decimal);
                sps[7].Value = P.MinSuttle;
                sps[8] = new SqlParameter("@UseCarReaderSystem", SqlDbType.Int);
                sps[8].Value = P.UseCarReaderSystem;
                sps[9] = new SqlParameter("@UseHisTareCompare", SqlDbType.Bit);
                sps[9].Value = P.UseHisTareCompare;
                sps[10] = new SqlParameter("@UnitHisTareCompare", SqlDbType.Int);
                sps[10].Value = P.UnitHisTareCompare;
                sps[11] = new SqlParameter("@QtyHisTareCompare", SqlDbType.Decimal);
                sps[11].Value = P.QtyHisTareCompare;
                sps[12] = new SqlParameter("@CompleteDirection", SqlDbType.Int);
                sps[12].Value = P.CompleteDirection;
                sps[13] = new SqlParameter("@CancelDirection", SqlDbType.Int);
                sps[13].Value = P.CancelDirection;

                StringBuilder sql = new StringBuilder();
                sql.Append("SET NOCOUNT ON\r\n");
                sql.Append("DECLARE @FID INT\r\n SET @FID=IsNull((SELECT 1 FROM KEW_SalesConfig WHERE FNumber=@Number),0)");
                sql.Append("IF @FID>0 BEGIN\r\n");
                sql.Append("  UPDATE KEW_SalesConfig SET FName=@Name WHERE FNumber=@Number\r\n");
                sql.Append("  UPDATE KEW_SalesConfigEntry SET FName=@Name,FCheckBox=@IsBuildStockOut,FCheckBox1=@NeedCheckOut,FInteger=@MinSpan,FDecimal=@EmptyMinSpan,FDecimal1=@MinSuttle,FComboBox3=@UseCarReaderSystem,");
                sql.Append("  FCheckBox2=@UseHisTareCompare,FDecimal2=@QtyHisTareCompare,FComboBox4=@UnitHisTareCompare,FComboBox1=@CompleteDirection,FComboBox2=@CancelDirection WHERE FNumber=@Number\r\n");
                sql.Append("  SELECT 'U',@FID\r\n");
                sql.Append("END \r\nELSE BEGIN\r\n");
                sql.Append("  EXEC GetICMaxNum 'KEW_SalesConfig',@FID OUTPUT\r\n");
                sql.Append("  INSERT INTO KEW_SalesConfig ([FID],[FNumber],[FName],[FParentID],[FLogic],[FDetail],[FDiscontinued],[FLevels],[FFullNumber],[FClassTypeID]) VALUES (@FID,@Number,@Name,0,-1,1,0,1,@Number,236900120)\r\n");
                sql.Append("  INSERT INTO KEW_SalesConfigEntry ([FClassTypeID],[FID],[FIndex],[FNumber],[FName],[FCheckBox],[FCheckBox1],[FInteger],[FDecimal],[FDecimal1],[FBase],[FBase1],[FComboBox1],[FComboBox2],[FComboBox3],[FCheckBox2],[FDecimal2],[FBase2],[FComboBox4],[FText]) ");
                sql.Append("  VALUES (236900120,@FID,0,@Number,@Name,@IsBuildStockOut,@NeedCheckOut,@MinSpan,@EmptyMinSpan,@MinSuttle,0,0,@CompleteDirection,@CancelDirection,@UseCarReaderSystem,@UseHisTareCompare,@QtyHisTareCompare,0,@UnitHisTareCompare,@ID)\r\n");
                sql.Append("  SELECT 'I',@FID\r\n");
                sql.Append("END \r\n");
                sql.Append("SET NOCOUNT OFF\r\n");
                Recordset rs = handler.Data.ExeProcedure(P.BaseNo, sql.ToString(), sps);
                if (rs.Enable)
                {
                    if (rs.Data.Tables[0].Rows[0][0].ToString() == "U")
                    {
                        result.Result = EnumResult.Update;
                        result.Content = rs.Data.Tables[0].Rows[0][1].ToString();
                        result.Message = String.Format("销售计量方案同步完成:{0},{1}", P.Number, result.Result.ToString());
                    }
                    else
                    {
                        result.Result = EnumResult.Insert;
                        result.Content = rs.Data.Tables[0].Rows[0][1].ToString();
                        result.Message = String.Format("销售计量方案同步完成:{0},{1}", P.Number, result.Result.ToString());
                    } 
                }
                else
                {
                    result.Result = EnumResult.None;
                    result.Message = rs.Message;
                    result.Message = String.Format("销售计量方案同步失败:{0}", rs.Message); 
                }
            }
            catch (Exception ex)
            {
                result.Result = EnumResult.None;
                result.Message = ex.Message;
            }
        }

    }

    /// <summary>
    /// 销售计量方案
    /// </summary>
    public class E01A01P
    {
        /// <summary>
        /// 账套编号
        /// </summary>
        public String BaseNo;
        /// <summary>
        /// 三方内码
        /// </summary>
        public String ID;
        /// <summary>
        /// 三方编号
        /// </summary>
        public String Number;
        /// <summary>
        /// 三方名称
        /// </summary>
        public String Name;
        /// <summary>
        /// 是否生成销售出库单（默认不生成）
        /// </summary>
        public Boolean IsBuildStockOut;
        /// <summary>
        /// 是否需要签发员签发
        /// </summary>
        public Boolean NeedCheckOut;
        /// <summary>
        /// 最小计量间隔
        /// </summary>
        public int MinSpan;
        /// <summary>
        /// 空车出厂最小间隔(吨)
        /// </summary>
        public Decimal EmptyMinSpan;
        /// <summary>
        /// 最小净重(吨)
        /// </summary>
        public Decimal MinSuttle;
        /// <summary>
        /// 是否启用车牌识别系统(0禁用，1仅记录，2进厂对比，3出厂对比)
        /// </summary>
        public int UseCarReaderSystem;
        /// <summary>
        /// 是否启用历史皮重对比
        /// </summary>
        public Boolean UseHisTareCompare;
        /// <summary>
        /// 历史皮重比较单位（0：吨。1：百分比）
        /// </summary>
        public int UnitHisTareCompare;
        /// <summary>
        /// 历史皮重比较数值
        /// </summary>
        public Decimal QtyHisTareCompare;
        /// <summary>
        /// 计量完毕驶出方向（0：按照工控机设置。1：全部抬起。2：抬起1#。3：抬起2#。4：抬起前方道闸。5：抬起后方道闸）
        /// </summary>
        public int CompleteDirection;
        /// <summary>
        /// 取消计量驶出方向（0：按照工控机设置。1：全部抬起。2：抬起1#。3：抬起2#。4：抬起前方道闸。5：抬起后方道闸）
        /// </summary>
        public int CancelDirection;
    }
}
